import 'dart:convert';
import 'package:dio/dio.dart';
import '../utils/log_utils.dart';

class LogsInterceptors extends InterceptorsWrapper {
  @override
  onRequest(
    RequestOptions options,
    RequestInterceptorHandler handler,
  ) {
    print("\n================== 请求数据 ==========================");
    print("|请求baseUrl：${options.baseUrl}");
    print("|请求url：${options.path}");
    print('|请求头: ');
    LogUtils.printMap(options.headers);
    print('|请求参数: ');
    LogUtils.printMap(options.queryParameters);
    print('|请求方法: ' + options.method);
    print("|contentType = ${options.contentType}");
    print('|请求时间: ' + DateTime.now().toString());
    if (options.data != null) {
      print('|请求数据: ${options.data}');
      // LogUtils.printMap(options.data);
    }

    handler.next(options);
  }

  @override
  onResponse(
    Response response,
    ResponseInterceptorHandler handler,
  ) {
    print("\n|================== 响应数据 ==========================");

    if (response != null) {
      print("|url = ${response.requestOptions.path}");
      print("|code = ${response.statusCode}");
      print("|data =");
      // LogUtils.printMap(response.data);
      print('|返回时间: ' + DateTime.now().toString());
      print("\n");
    } else {
      print("|data = 请求错误 E409");
      print('|返回时间: ' + DateTime.now().toString());
      print("\n");
    }
    handler.next(response);
  }

  @override
  onError(
    DioError e,
    ErrorInterceptorHandler handler,
  ) {
    print("\n================== 错误响应数据 ======================");
    print("|url = ${e.requestOptions.path}");
    print("|type = ${e.type}");
    print("|message = ${e.message}");

    print('|response = ${e.response}');
    print("\n");

    handler.next(e);
  }
}
